package com.br4mmie.asciistory.ui;

import java.util.ArrayList;

import com.br4mmie.asciistory.ASCIIStoryActivity;
import com.br4mmie.asciistory.CharImage;
import com.br4mmie.asciistory.Keybindings;


/**
 * OptionWindow class, extension of the Window
 * this represents a window with multiple options the user can choose from.
 * The input is handled by a listener (SelectionListener) which gets an integer value from the window
 * 0 is the default unselected value, options start counting at 1.
 * Options can be added with .addOption(String), where String is the options representation in text  
 * @author Niels
 * 
 */
public class OptionWindow extends Window{
	/**
	 * SelectionListener.
	 * handles the selections, by getting the entered option, in integer value.
	 * @author Niels
	 *
	 */
	public interface SelectionListener
	{
		public void select(int option);
	}
	
	private int currentSelection;//the currently selected option
	private int selectedOption; //the selected option, when enter is pressed this is set to the highlighted option, if this is not 0 the window registers a choice has been made
	private ArrayList<WindowOption> options; //The options the user can choose from
	private SelectionListener listener; //the Listener attached to this window
	private String text; //the title or question for the window to hold
	
	/**
	 * Constructor
	 * @param x windows' x position
	 * @param y windows' y position
	 * @param w windows' width
	 * @param h windows' height
	 * @param t windows' title
	 * @param listener' listener to attach to the window
	 */
	public OptionWindow(int x, int y, int w, int h, String t, SelectionListener listener)
	{
		super(x, y, w, h);
		text = "";
		text = t;
		selectedOption = 0;
		currentSelection = 0;
		canBeClosed = false; //selection has to be made, can't close window
		options = new ArrayList<WindowOption>();
		this.listener = listener;
	}
	
	/**
	 * Checks if a choice has been made
	 */
	public void getOption()
	{
		if(selectedOption == 0)return;
		else
		{
			listener.select(selectedOption);
			try
			{
				Thread.sleep(ASCIIStoryActivity.keyDelay);
			}
			catch (Exception ex) {}
			close();
		}
		
	}
	
	/**
	 * addOption : add an option to the option window
	 * @param optionText
	 */
	public void addOption(String optionText)
	{
		WindowOption option = new WindowOption(optionText);
		currentSelection = 1;
		options.add(option);
	}

	/**
	 * Basic onLoop function	
	 */
	public void onLoop()
	{
		super.onLoop();
		getOption();
		
	}
	
	/**
	 * basic draw function, draws the window to the given CharImage
	 */
	public void draw(CharImage target)
	{
		if(isClosed)return;
		clear();
		String tempstring = "";
		tempstring += text + '\n'+'\n';
		if(!options.isEmpty())
		{
			for(WindowOption o : options)
			{
				tempstring += o.getLine();
			}
		
		}
		image.writeImage(tempstring,1,1);
		super.draw(target);
	}
	
	/**
	 * Handles events
	 */
	public void handleEvents()
	{
		super.handleEvents();
		getKeyInput();
	}
	
	/**
	 * Handles Keyboard input
	 */
	public void getKeyInput()
	{
		for(WindowOption o : options)o.isSelected = false;
		boolean keypressed = false;
		
		if(options.isEmpty())return;
		if(Keybindings.UP.pressed() || Keybindings.RIGHT.pressed())
		{
			if(currentSelection <= 1)currentSelection = options.size();
			else currentSelection--;
			keypressed = true;
		}
		if(Keybindings.DOWN.pressed() || Keybindings.LEFT.pressed())
		{
			if(currentSelection >= options.size())currentSelection = 1;
			else currentSelection++;
			keypressed = true;
		}
		if(currentSelection != 0)options.get(currentSelection-1).isSelected = true;
		if(Keybindings.INTERACT.pressed())selectedOption = currentSelection;
		
		if(keypressed)
		{
			try
			{
				Thread.sleep(ASCIIStoryActivity.keyDelay);
			}
			catch (Exception ex) {}
		}
	}
	
	/**
	 * Closes the window
	 */
	public void close()
	{
		super.close();
	}

}
